using System;
using System.Windows.Forms;
using System.IO;
using BusinessLayer;
using dpatoolkit.UILayer;
namespace dpatoolkit
{
	/// <summary>
	/// Static class which takes care of creating the global items.. like the menu items of 
	/// design patterns is created by refering to the file system.
	/// </summary>
	public class GlobalFactory
	{
		public static MenuItem menuItemCreational,
			menuItemStructural,
			menuItemBehavioral,
			menuItemAddFromFile;
	static GlobalFactory()
	{
		string pathToDPRepo = GlobalSettings.SampleDPPluginPath;
	}

	public static MenuItem[] CreateDesignPatternMenuItems()
	{
		MenuItem[] menuitems = new MenuItem[3];//3 for each category 
		string pathToDPRepo = GlobalSettings.SampleDPPluginPath;
		string creationalDPDir = pathToDPRepo+@"\" + GlobalSettings.Creational;
		string structDPDir = pathToDPRepo+@"\" + GlobalSettings.Structural;
		string behavDPDir = pathToDPRepo+@"\" + GlobalSettings.Behavioral;
		
		//menuitems[0] = new MenuItem("creational");
		checkAndAdd(out menuitems[0],creationalDPDir,GlobalSettings.Creational);
		//menuitems[1] = new MenuItem("structural");
		checkAndAdd(out menuitems[1],structDPDir,GlobalSettings.Structural);
		
		checkAndAdd(out menuitems[2],behavDPDir,GlobalSettings.Behavioral);
		return menuitems;
		/*
		 if(Directory.Exists(creationalDPDir))
		{
			menuitems[0]=new MenuItem("creational");
			string[] files = Directory.GetFiles(creationalDPDir,"*"+GlobalSettings.DPExtension);
			foreach(string file in files)
			{
				string patName = file.Replace(GlobalSettings.DPExtension,"");
				menuitems[0].MenuItems.Add(patName);
			}
		}
		else
		{
		
		}
		*/


	}
		private static void checkAndAdd(out MenuItem menuitem,string dir,string menuname)
		{
			if(Directory.Exists(dir))
			{
				menuitem = new MenuItem(menuname);
				
				string[] files = Directory.GetFiles(dir,"*"+GlobalSettings.DPExtension);
				foreach(string file in files)
				{
					string patName = file.Replace(GlobalSettings.DPExtension,"");
					string[] temp = patName.Split('\\');
					menuitem.MenuItems.Add(temp[temp.GetLength(0)-1]);
				}
			}
			else
			{
				ErrorMessage.Show("The Directory corresponding to Design Pattern category :"+menuname +" was not found!\n"+"path to dir:"+dir+"\nSelect Add from file menu option to choose and add it from the file manually");
				menuitem=new MenuItem("CouldNot Find category:"+menuname);
			}
		
		}
		public static string ConstructPathToPattern(string category,string patternName)
		{
			return GlobalSettings.SampleDPPluginPath+@"\"+category+@"\"+patternName+GlobalSettings.DPExtension;
		}


	}
}
